package com.hspedu.generic;

import java.util.ArrayList;

/**
 * @Author Stringzhua
 * @Date 2024/3/13 20:53
 * description:
 * 请编写程序,在ArrayList中,添加3个Dog对象
 * Dog对象含有name和age,并输出name和age(要求使用getXxx())
 */
@SuppressWarnings("all")
public class Generic01 {
    public static void main(String[] args) {
        //使用传统方式解决
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("旺财", 10));
        arrayList.add(new Dog("发财", 1));
        arrayList.add(new Dog("小黄", 5));

        //假如我们的程序员不小心加入了一只猫
        arrayList.add(new Cat("招财猫", 8));

        //遍历
        for (Object o : arrayList) {
            //向下转型
            Dog dog = (Dog) o;//ClassCastException 类型转换异常
            System.out.println(dog.getName() + "-" + dog.getAge());
        }

    }
}

class Dog {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat {
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
